﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn;
    string _new="";
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Institte_of_Fine_ArtsConnectionString"].ConnectionString);

    }

    protected bool CheckExistStudent(string StudentID)
    {
        conn.Open();
        SqlCommand comm = new SqlCommand();
        comm.CommandText = "SELECT StudentID FROM Student WHERE StudentID=" + StudentID;
        comm.CommandType = System.Data.CommandType.Text;
        comm.Connection = conn;
        SqlDataReader read = comm.ExecuteReader();
        if (read.HasRows != null)
        {
            return false;
        }
        return true;
    }


    protected void gvwStudent_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
    {
        Regex rx;
        if (e.NewValues["StudentID"] == null)
        {
            AddError(e.Errors, gvwStudent.Columns["StudentID"], "Staff ID can't be null.");
        }
        else
        {
            if (e.NewValues["StudentID"].ToString().Length < 11)
            {
                AddError(e.Errors, gvwStudent.Columns["StudentID"], "Staff ID must be at least 11 characters long.");
            }
            else
            {
                if (e.NewValues["StudentID"].ToString().Length > 13)
                {
                    AddError(e.Errors, gvwStudent.Columns["StudentID"], "Staff ID must be at more than 13 characters long.");
                }
                else
                {
                    string StudentID = "^S[0-9]{6}-G|H|K[0-9]{2,3}$";
                    rx = new Regex(StudentID, RegexOptions.IgnoreCase);
                    if (e.NewValues["StudentID"] != null && !rx.IsMatch(e.NewValues["StudentID"].ToString()))
                    {
                        AddError(e.Errors, gvwStudent.Columns["StudentID"], "Staff ID invalid. (Ex: S010113-G01,S010113-H01,S010113-K01)");
                    }
                    else
                    {
                        _new = e.NewValues["StudentID"].ToString();
                    }
                }
            }
            if (e.NewValues["Password"] == null)
            {
                AddError(e.Errors, gvwStudent.Columns["Password"], "Password can't be null.");
            }
            else
            {
                if (e.NewValues["Password"].ToString().Length < 6)
                {
                    AddError(e.Errors, gvwStudent.Columns["Password"], "Password must be at least 6 characters long.");
                }
                else
                {
                    if (e.NewValues["Password"].ToString().Length > 32)
                    {
                        AddError(e.Errors, gvwStudent.Columns["Password"], "Password must be more than 32 characters long.");
                    }
                }
            }

            if (e.NewValues["Firstname"] == null)
            {
                AddError(e.Errors, gvwStudent.Columns["Firstname"], "First name can't be null.");
            }

            if (e.NewValues["Lastname"] == null)
            {
                AddError(e.Errors, gvwStudent.Columns["Lastname"], "Last name can't be null.");
            }
            if (e.NewValues["Birthdate"] == null)
            {
                AddError(e.Errors, gvwStudent.Columns["Birthdate"], "Birthdate can't be null.");
            }
            else
            {
                DateTime birthdate = Convert.ToDateTime(e.NewValues["Birthdate"].ToString());
                if (birthdate.Year > DateTime.Today.Year)
                {
                    AddError(e.Errors, gvwStudent.Columns["Birthdate"], "Birthdate must be at least today");
                }
            }
            if (e.NewValues["Phone"] == null)
            {
                AddError(e.Errors, gvwStudent.Columns["Phone"], "Phone can't be null.");
            }

            string phone = "^(0\\d{10})$|(0\\d{9})";
            rx = new Regex(phone, RegexOptions.IgnoreCase);
            if (e.NewValues["Phone"] != null && !rx.IsMatch(e.NewValues["Phone"].ToString()))
            {
                AddError(e.Errors, gvwStudent.Columns["Phone"], "Phone invalid! Ex:01678885070 or 0900111222 ");
            }
            if (e.NewValues["Address"] == null)
            {
                AddError(e.Errors, gvwStudent.Columns["Address"], "Address can't be null.");
            }
            if (e.NewValues["Email"] == null)
            {
                AddError(e.Errors, gvwStudent.Columns["Email"], "Address can't be null.");
            }
            string email = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
            rx = new Regex(email, RegexOptions.IgnoreCase);
            if (e.NewValues["Email"] != null && !rx.IsMatch(e.NewValues["Email"].ToString()))
            {
                AddError(e.Errors, gvwStudent.Columns["Email"], "Email invalid! Ex:emailname@gmail.com ");
            }
        }

    }

        void AddError(Dictionary<GridViewColumn, string> errors, GridViewColumn column, string errorText)
        {
            if (errors.ContainsKey(column)) return;
            errors[column] = errorText;
        }
}
    


    

